package com.yc.d0303;

import com.google.gson.Gson;
import com.yc.bean.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddCartServlet", value = "/AddCartServlet")
public class AddCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        final String name = request.getParameter("name");
        final HttpSession session = request.getSession();

        Integer nums = (Integer) session.getAttribute(name);
        if (nums == null){
            nums = 0;
        }
        nums++;
        session.setAttribute(name ,nums);

        Result res = new Result(1,"添加成功",null);
        final String json = new Gson().toJson(res);
        response.getWriter().append(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
